/**
 * @author Vitor Franco do Carmo (vitor.carmo@cpmbraxis.com)
 * @since 02/08/2013
 *
 */
package br.com.vfcfw.test;

import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;

/**
 * Classe responsável por disponibilizar a injeção de dependências usando CDI nos testes unitários.
 * 
 * @author Vitor Franco do Carmo (vitor.carmo@cpmbraxis.com)
 * @since 02/08/2013
 * 
 */
public class WeldJUnit4Runner extends BlockJUnit4ClassRunner {

    private final Class<?> classe;

    private final Weld weld;

    private final WeldContainer container;

    /**
     * Construtor da classe.
     * 
     * @param classe
     * @throws InitializationError
     */
    public WeldJUnit4Runner(Class<?> classe) throws InitializationError {

        super(classe);
        this.classe = classe;
        this.weld = new Weld();
        this.container = weld.initialize();
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.junit.runners.BlockJUnit4ClassRunner#createTest()
     */
    @Override
    protected Object createTest() throws Exception {

        final Object test = container.instance().select(classe).get();
        return test;
    }
}
